<?php

namespace App\Service;

use App\Constants\ErrorCode;
use App\Library\Common;
use App\Model\LinkIndustry;
use App\Model\Topic;
use Hyperf\DbConnection\Db;
use Hyperf\Utils\ApplicationContext;
use Hyperf\Di\Annotation\Inject;

class TopicService extends BaseService
{
    /**
    * @param array $params
    * @return object
    */
    public function get_list($params)
    {
        $fields = ['*'];
        $key_word = isset($params['key_word'])?$params['key_word']:'';
        $is_show = isset($params['is_show'])?$params['is_show']:'';
        $type = isset($params['type'])?$params['type']:'1';
        $ids = $params['ids']?$params['ids']:[];
        $where = [];
        $where['type'] = $type;
        if($is_show!==''){
            $where['is_show'] = $is_show;
        }
        $list = [];
        return  Topic::query()
                ->where($where)
                ->where(function ($query) use ($key_word) {
                    if (!empty($key_word)) {
                        $query->where('topic_name', 'like', '%' . $key_word . '%');
                    }
                })
                ->where(function ($query) use ($ids) {
                    if ($ids!==[]) {
                        $query->whereIn('id',$ids);
                    }
                })
                ->with(['create_info','update_info','province','city','area','street','link_industry','file_info'])
                ->orderBy('sort','desc')
                ->orderBy('id','desc')
                ->select();
    }

    /**
     * @param int $id
     * @return object
     *
     */
    public function info($id)
    {
        return Topic::query()
            ->where('id',$id)
            ->with(['create_info','update_info','province','city','area','street','link_industry'])
            ->first();
    }

    /**
     * 新增单条  包括关联关系
     * @param array $insertData
     * @return bool
     *
     */
    public function insert(array  $insertData)
    {
        $industry =  [];
        if(isset($insertData['industry'])){
            $industry  = $insertData['industry'];
            unset($insertData['industry']);
        }
        $id = Topic::query()->insertGetId(Common::filter_null($insertData));
        $industry_data = [];
        foreach ($industry as $v){
            $industry_data[] = [
                'industry_id'=>$v,
                'link_id'=>$id,
                'type'=>7,
            ];
        }
        if(count($industry_data)>0){
            LinkIndustry::query()->insert($industry_data);
        }
        return  $id;
    }

    /**
     * 新增单条  包括关联关系
     * @param array $insertData
     * @return bool
     *
     */
    public function insertGetId(array  $insertData)
    {
        return Topic::query()->insertGetId($insertData);
    }

//    更新包括关联关系
    public function update(array  $upData)
    {
//        先清空旧数据
        LinkIndustry::query()->where(['type'=>7,'link_id'=>$upData['id']])->delete();
        $industry = [];
        if(isset($upData['industry'])){
            $industry = $upData['industry'];unset($upData['industry']);
        }
        $industry_data  = [];
        foreach ($industry as $v){
            $industry_data[] = [
                'industry_id'=>$v,
                'link_id'=>$upData['id'],
                'type'=>7,
            ];
        }
        if(count($industry_data)>0){
            LinkIndustry::query()->insert($industry_data);
        }
        return Topic::query()->where('id',$upData['id'])->update(Common::filter_null($upData));
    }

//    查找全部行业下的企业ids
    public function linkfind(array $ids)
    {
        $link_id = LinkIndustry::query()->where('type',4)->whereIn('industry_id',$ids)->pluck('link_id');
        if($link_id){
            return $link_id->toArray();
        }else{
            return [];
        }
    }
}